package com.zhoug.player.exoplayer.v2;


import com.zhoug.player.core.utils.LogUtils;

import java.util.Arrays;

import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.TransferListener;

/**
 * 数据传输事件监听器 默认实现
 *
 * @author: zhoug
 * @since: 2025-06-20
 */
@UnstableApi
public class ZDefaultTransferListener implements TransferListener {
    private static final String TAG = ">>>TransferListener";

    /**
     * 在初始化传输时调用
     *
     * @param source    The source performing the transfer.
     * @param dataSpec  Describes the data for which the transfer is initialized.
     * @param isNetwork Whether the data is transferred through a network.
     */
    @Override
    public void onTransferInitializing(DataSource source, DataSpec dataSpec, boolean isNetwork) {
            LogUtils.d(TAG, "onTransferInitializing:正在初始化资源 "
                    + "isNetwork:" + isNetwork
                    + ",position:" + dataSpec.position
                    + ",length:" + dataSpec.length
                    + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                    + ",key:" + dataSpec.key
                    + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                    + ",uri:" + source.getUri()

            );
    }

    /**
     * 在传输开始时调用
     *
     * @param source    The source performing the transfer.
     * @param dataSpec  Describes the data being transferred.
     * @param isNetwork Whether the data is transferred through a network.
     */
    @Override
    public void onTransferStart(DataSource source, DataSpec dataSpec, boolean isNetwork) {
            LogUtils.d(TAG, "onTransferStart:传输开始 "
                    + "isNetwork:" + isNetwork
                    + ",position:" + dataSpec.position
                    + ",length:" + dataSpec.length
                    + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                    + ",key:" + dataSpec.key
                    + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                    + ",uri:" + source.getUri()

            );


    }

    @Override
    public void onBytesTransferred(DataSource source, DataSpec dataSpec, boolean isNetwork, int bytesTransferred) {
        LogUtils.d(TAG, "onBytesTransferred:传输中 "
                + "isNetwork:" + isNetwork
                + ",position:" + dataSpec.position
                + ",length:" + dataSpec.length
                + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                + ",key:" + dataSpec.key
                + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                + ",bytesTransferred:" + bytesTransferred
                + ",uri:" + source.getUri()
        );

    }

    @Override
    public void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork) {
        LogUtils.d(TAG, "onTransferEnd:传输结束 "
                + "isNetwork:" + isNetwork
                + ",position:" + dataSpec.position
                + ",length:" + dataSpec.length
                + ",uriPositionOffset:" + dataSpec.uriPositionOffset
                + ",key:" + dataSpec.key
                + ",httpBody:" + Arrays.toString(dataSpec.httpBody)
                + ",uri:" + source.getUri()
        );
    }

}
